package entity;

import javax.sql.rowset.serial.SerialArray;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.Serializable;

public class Student implements Serializable {
	// 若没有声明serialVersionID ，会默认声明一个
	// 而一旦修改了类结构，则会发生变化，也就匹配ID错误，保证恢复的是同一个数据。
	private static final long serialVersionUID = -4392658638228508589L;


	// 静态字符串是不会被序列化的
	// 因为序列化保存的是对象的信息，而非类的信息，对与静态属性，是类里面的一些信息。不会序列化，这个也合情合理
	private static String staticField = "静态字符串";


	// transient 目的就是为了避免被序列化
	// 例如一些私有的属性（密码字段），就可以利用transient修饰符来隐藏起来
	private transient String transientField = "transient字符串";
	private String name;
	private Integer age;
	private Integer score;

	private void readObject(ObjectInputStream objectInputStream) {
		try {
			objectInputStream.defaultReadObject();
			if (0 > score || 100 < score) {
				throw new IllegalArgumentException("学生分数，只能存在0-100之间。");
			}

		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

	public Student() {
	}

	public Student(String name, Integer age, Integer score, String transientField) {
		this.transientField = transientField;
		this.name = name;
		this.age = age;
		this.score = score;
	}

	public Student(String name, Integer age, Integer score) {
		this.name = name;
		this.age = age;
		this.score = score;
	}

	@Override
	public String toString() {
		return "Student{" +
				"name='" + name + '\'' +
				", age=" + age +
				", score=" + score +
				'}';
	}

	public String getTransientField() {
		return transientField;
	}

	public void setTransientField(String transientField) {
		this.transientField = transientField;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Integer getAge() {
		return age;
	}

	public void setAge(Integer age) {
		this.age = age;
	}

	public Integer getScore() {
		return score;
	}

	public void setScore(Integer score) {
		this.score = score;
	}
}
